package com.home.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @Description:时间处理的函数
 * @createBy：ran-qi-qiang
 * @Package:com.pccw.util
 * @ClassName:DateUtil
 */
public class DateUtil extends CommonUtil{
	/**
	 * 
	 * @Description:计算两个时间差几个月
	 * @createBy:qiqiang ran
	 * @createdate:2012-3-27
	 * @param endTime
	 * @param startTime
	 * @return
	 * @throws ParseException
	 */
	public static int getMinDate(String endTime, String startTime) throws ParseException {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar ca1 = Calendar.getInstance();
		Calendar ca2 = Calendar.getInstance();
		Date d1 = df.parse(startTime);
		Date d2 = df.parse(endTime);
		ca1.setTime(d1);
		ca2.setTime(d2);
		return (ca2.get(Calendar.YEAR) - ca1.get(Calendar.YEAR)) * 12 + ca2.get(Calendar.MONTH) - ca1.get(Calendar.MONTH)+1;
	}
	
	/**
	 * 
	 * @Description:获得将时间期间，按月份，返回数组
	 * @createBy:ran-qi-qiang
	 * @param endTime
	 * @param startTime
	 * @return
	 * @throws ParseException 
	 */
	public static String[] getPeriodByMonth(String endTime, String startTime) throws ParseException{
		int total = getMinDate(endTime, startTime);
		String [] allMonths = new String[total];
		for(int i = 0;i<total;i++){
			allMonths[i] = nextMonth(startTime,i);
		}
		return allMonths;
	}
	/**
	 * 
	 * @Description:计算目标时间的下一个月
	 * @createBy:ran-qi-qiang
	 * @param targetTime
	 * @return
	 */
	public static String nextMonth(String targetTime,int addMonth){
		Date date = StringToDate(targetTime);
		return DateToString(nextMonth(date,addMonth));
	}
	
	/**
	 * 
	 * @Description:计算目标时间的几个月
	 * @createBy:ran-qi-qiang
	 * @createdate:2012-6-27
	 * @param targetTime
	 * @return
	 */
	public static Date nextMonth(Date targetTime,int addMonth){
		Calendar ca1 = Calendar.getInstance();
		ca1.setTime(targetTime);
		ca1.add(Calendar.MONTH, addMonth);
		return ca1.getTime();
	}
	/**
	 * 
	 * @Description:计算目标时间的下一个月
	 * @createBy:ran-qi-qiang
	 * @createdate:2012-6-27
	 * @param targetTime
	 * @return
	 */
	public static Date nextMonth(Date targetTime){
		Calendar ca1 = Calendar.getInstance();
		ca1.setTime(targetTime);
		ca1.add(Calendar.MONTH, 1);
		return ca1.getTime();
	}
	
	/**
	 * 
	 * @Description:将yyyy-MM-dd字符串转换成Date类型,yyyy-MM
	 * @createBy:ran-qi-qiang
	 * @param time
	 * @return
	 */
	public static Date StringToDate(String time){
		DateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return sf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 
	 * @Description:将Date 转换成String yyyy-MM-dd类型
	 * @createBy:ran-qi-qiang
	 * @createdate:2012-5-17
	 * @param date
	 * @return
	 */
	public static String DateToString(Date date){
		DateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		return sf.format(date);
	}
	
	/**
	 * 
	 * @Description:将Long类型转换成Date 类型
	 * @createBy:ran-qi-qiang
	 * @param time
	 * @return
	 */
	public static Date LongToDate(long time){
		Date date = new Date(time);
		return date;
	}
	
	/**
	 * 
	 * @Description:将Long转换成时间字符串
	 * @createBy:ran-qi-qiang
	 * @param time
	 * @return
	 */
	public static String LongToString(long time){
		return DateToString(LongToDate(time));
	}
	
	/**
	 * 
	 * @Description:DateToLong
	 * @createBy:ran-qi-qiang
	 * @param date
	 * @return
	 */
	public static long DateToLong(Date date){
		return date.getTime();
	}
	/**
	 * 
	 * @Description:StringToLong
	 * @createBy:ran-qi-qiang
	 * @param time
	 * @return
	 */
	public static long StringToLong(String time){
		return StringToDate(time).getTime();
	}
	
	/**
	 * 
	 * @Description:传入yyyy-MM-dd 格式的时间字符串
	 * 				判断目标时间是否在开始时间到结束时间之间（含等于）
	 * @createBy:ran-qi-qiang
	 * @createdate:2012-5-25
	 * @param targetTime
	 * @param StartTime
	 * @param endTime
	 * @return
	 */
	public static boolean isBetweenTime(String targetTime,String startTime,String endTime){
		long tTime = StringToLong(targetTime);
		long sTime = StringToLong(startTime);
		long eTime = StringToLong(endTime);
		if(tTime>=sTime&&tTime<=eTime){
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * @Description:传入yyyy-MM-dd 时间字符串，判断目标时间是否是，前一个时间的下一个月
	 * @createBy:ran-qi-qiang
	 * @param targetTime
	 * @param preTime
	 * @return
	 */
	public static boolean isNextMonth(String targetTime,String preTime){
		try {
			if(getMinDate(targetTime, preTime) == 1){
				return true;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 
	 * @Description:计算目标时间的下一个月
	 * @createBy:ran-qi-qiang
	 * @param targetTime
	 * @return
	 */
	public static String nextMonth(String targetTime){
		Date date = StringToDate(targetTime);
		return DateToString(nextMonth(date));
	}
	
	/**
	 * 
	 * @Description:传入yyyy-MM-dd 时间，判断目标时间 是否大于 与之想比较的时间
	 * @createBy:ran-qi-qiang
	 * @param targetTime
	 * @param compateTime
	 * @return
	 */
	public static boolean isBigerThanOther(String targetTime,String compateTime){
		long tTime = StringToLong(targetTime);
		long cTime = StringToLong(compateTime);
		if(tTime>cTime){
			return true;
		}
		return false;
	}
}
